package com.ujn.zhxy.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ujn.zhxy.mapper.AdminMapper;
import com.ujn.zhxy.mapper.StudentMapper;
import com.ujn.zhxy.pojo.Admin;
import com.ujn.zhxy.pojo.LoginFrom;
import com.ujn.zhxy.pojo.Student;
import com.ujn.zhxy.service.AdminService;
import com.ujn.zhxy.service.StudentService;
import com.ujn.zhxy.util.MD5;
import com.ujn.zhxy.util.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service("studentServiceImpl") //括号内为id
@Transactional //事务控制
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    @Override
    public Student login(LoginFrom loginFrom) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",loginFrom.getUsername())
                .eq("password", MD5.encrypt(loginFrom.getPassword()));
        Student student = baseMapper.selectOne(queryWrapper);
        return student;
    }

    @Override
    public Student getStudentById(Long userId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",userId);

        Student student = baseMapper.selectOne(queryWrapper);
        return student;
    }

    @Override
    public IPage<Student> getStudentByOpr(Page<Student> pagePram, Student student) {
        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(student.getName())){
            studentQueryWrapper.like("name",student.getName());
        }
        if (!StringUtils.isEmpty(student.getClazzName())){
            studentQueryWrapper.like("clazz_name",student.getClazzName());
        }
        studentQueryWrapper.orderByDesc("id");
        Page<Student> studentPage = baseMapper.selectPage(pagePram, studentQueryWrapper);
        return studentPage;
    }
    //ServiceImpl<M,T>包含了针对于IService的实现
}
